package org.song.videoplayer.rederview;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import org.song.videoplayer.media.IMediaControl;

/**
 * Contact github.com/tohodog
 * 小于4.0用这个绘制view
 * ps:切换全屏效果不好 会停顿
 * 性能较好
 */
public class SurfaceRenderView extends SurfaceProvider implements SurfaceOps.Callback, IRenderView, Component.EstimateSizeListener {

    protected static final String TAG = "SufaceRenderView";
    private MeasureHelper mMeasureHelper;
    private int videoWidth;
    private int videoHeight;
    private static final int DOMAIN = 0x00101;
    private HiLogLabel logLabel = new HiLogLabel(HiLog.INFO, DOMAIN, TAG);

    private IRenderCallback callback;

    public SurfaceRenderView(Context context) {
        super(context);
        init();
    }

    public SurfaceRenderView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        if (getSurfaceOps().isPresent()) {
            getSurfaceOps().get().addCallback(this);
            //getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
            pinToZTop(false);
            mMeasureHelper = new MeasureHelper(this);
            setEstimateSizeListener(this);
        }
    }


    @Override
    public Component get() {
        return this;
    }

    @Override
    public void setVideoSize(int videoWidth, int videoHeight) {
        if (videoWidth >= 0 && videoHeight >= 0) {
            this.videoWidth = videoWidth;
            this.videoHeight = videoHeight;
            if (holder != null)
                holder.setFixedSize(videoWidth, videoHeight);
            mMeasureHelper.setVideoSize(videoWidth, videoHeight);

            postLayout();
        }
    }

    @Override
    public void setAspectRatio(int aspectRatio) {
        mMeasureHelper.setAspectRatio(aspectRatio);
        postLayout();
    }

    @Override
    public void setVideoRotation(int degree) {
        //不支持
    }

    @Override
    public void addRenderCallback(IRenderCallback callback) {
        this.callback = callback;
    }

    @Override
    public void removeRenderCallback() {
        this.callback = null;
    }


    @Override
    public SurfaceOps getSurfaceHolder() {
        return holder;
    }

    @Override
    public Surface openSurface() {
        if (holder != null)
            return holder.getSurface();
        return null;
    }

    @Override
    public void bindMedia(IMediaControl iMediaControl) {
        //iMediaControl.setDisplay(null);
//        holder.setFormat(SurfaceOps.SURFACE_TYPE_PUSH_BUFFERS);
        iMediaControl.setDisplay(getSurfaceHolder());
    }

    @Override
    public PixelMap getCurrentFrame() {

        return null;
    }

    private SurfaceOps holder;


    @Override
    public void surfaceCreated(SurfaceOps holder) {
        HiLog.info(logLabel, "surfaceCreated");
        this.holder = holder;
        if (callback != null)
            callback.onSurfaceCreated(this, 0, 0);
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int format, int width, int height) {
        HiLog.info(logLabel, "surfaceChanged " + width + "-" + height);
        if (callback != null)
            callback.onSurfaceChanged(this, format, width, height);
    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {
        HiLog.info(logLabel, "surfaceDestroyed");
        if (callback != null)
            callback.onSurfaceDestroyed(this);
    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
        mMeasureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = mMeasureHelper.getMeasuredWidth();
        int measuredHeight = mMeasureHelper.getMeasuredHeight();
        setEstimatedSize(EstimateSpec.getSizeWithMode(measuredWidth, widthMeasureSpec),
                EstimateSpec.getSizeWithMode(measuredHeight, heightMeasureSpec));
        return true;
    }
}
